<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CMemberLogin extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	 public function __construct()
	    {
	        parent::__construct();
	        session_start();
	        $this->output->enable_profiler(FALSE);
	    }
	    
	public function index()
	{
		$data['title'] = "Welcome to Norton";
		$this->load->vars($data);
		$this->load->view('site/home_template');
	}
	
	
	public function changePassword() {
		if (!isset($_SESSION['uid']) || $_SESSION['uid'] < 1){
		   	redirect('cmemberlogin/requiredmessage','refresh');
		}
		/*$required_if = $this->input->post('newpassword') ? '|required' : '' ;
		$this->form_validation->set_rules('currentpassword', 'current password', 'trim'. $required_if .'|min_length[6]');           
		$this->form_validation->set_rules('newpassword', 'newpassword', 'trim|min_length[6]|matches[confirm_newpassword]');
		$this->form_validation->set_rules('confirm_newpassword', 'password confirmation', 'trim');*/
		
		if ($this->input->post('submit')){
	  		/*$this->MUser->changePassword();
	  		$this->session->set_flashdata('message','Password updated');
			
	  		if($_SESSION['role_id']==1) {			
	  			redirect('administration/index','refresh');
			}
	  		if($_SESSION['role_id']==2) {	
				redirect('cadmindepartment','refresh');
			}
	  		if($_SESSION['role_id']==3) {			
				redirect('clecturer','refresh');
			}*/
			
			$this->form_validation->set_rules('currentpassword', 'Current Password', 'required');
			$this->form_validation->set_rules('newpassword', 'New Password', 'required');
			$this->form_validation->set_rules('confirm_newpassword', 'Password Confirmation', 'required');
			
			if ($this->form_validation->run() == FALSE)
			{
				$data['title'] = "Change Password";
				$data['main'] = 'site/changepassword';
				$data['left'] = 'site/admin_department_left';	
				$this->load->vars($data);
				$this->load->view('site/template'); 
			}
			else
			{
				$this->load->view('formsuccess');
			}
	  		
	  	}else{
			$data['title'] = "Change Password";
			$data['main'] = 'site/changepassword';
	  		if($_SESSION['role_id']==1) {			
			
			}
	  		if($_SESSION['role_id']==2) {		
	  			$data['left'] = 'site/admin_department_left';	
			}
	  		if($_SESSION['role_id']==3) {			
	  			
			}
			$this->load->vars($data);
			$this->load->view('site/template');      
		}
	}
	
	public function setting() {
		if (!isset($_SESSION['uid']) || $_SESSION['uid'] < 1){
		   	redirect('cmemberlogin/requiredmessage','refresh');
		}
		
		if ($this->input->post('submit')){
	  		$this->MUser->updateUser($_SESSION['role_id']);
	  		$this->session->set_flashdata('message','Account updated');
			
	  		if($_SESSION['role_id']==1) {			
				$this->MUser->updateAdminUser();
	  			redirect('administration/index','refresh');
			}
	  		if($_SESSION['role_id']==2) {
	  			$this->MUser->updateAdminDepartmentUser();			
				redirect('cadmindepartment','refresh');
			}
	  		if($_SESSION['role_id']==3) {			
	  			$this->MUser->updateLecturerUser();
				redirect('clecturer','refresh');
			}
	  		
	  	}else{
			$data['title'] = "Account Setting";
			$data['main'] = 'site/account_setting';
			$data['roles'] = $this->MUser->getRoles();
			$data['setting'] = null;
			
			if($_SESSION['role_id']==1) {			
				$data['setting'] = $this->MUser->getAdminUser($_SESSION['uid']);
				if (!count($data['setting'])){
					redirect('administration/index','refresh');
				}
			}
	  		if($_SESSION['role_id']==2) {		
	  			$data['left'] = 'site/admin_department_left';	
				$data['setting'] = $this->MUser->getAdminDepartmentUser($_SESSION['uid']);
				if (!count($data['setting'])){
					redirect('cadmindepartment','refresh');
				}
			}
	  		if($_SESSION['role_id']==3) {			
	  			$data['left'] = 'site/lecturer_left';
				$data['setting'] = $this->MUser->getLecturerUser($_SESSION['uid']);
				if (!count($data['setting'])){
					redirect('clecturer','refresh');
				}
			}
			$this->load->vars($data);
			$this->load->view('site/template');      
		}
	}
	
	public function login() {
		if ($this->input->post('username')){
			$u = $this->input->post('username');
			$pw = $this->input->post('password');
			$this->MUser->verifyUser($u,$pw);
			if ($_SESSION['uid'] > 0){
				if($_SESSION['role_id']==1) {
					$this->session->set_flashdata('error', 'Sorry, your username or password is incorrect!');
				}
				else if ($_SESSION['role_id']== 2){
					redirect('cadmindepartment','refresh');
				}
				else if ($_SESSION['role_id']== 3){ 
					redirect('clecturer','refresh');
				}
			}
		}
		$data['title'] = "Member Login";
		$data['main'] = 'site/login';
		$this->load->vars($data);
		$this->load->view('site/template');  
	}
	
	function logout(){
		unset($_SESSION['uid']);
		unset($_SESSION['role_id']);
		unset($_SESSION['username']);
		$this->session->set_flashdata('error',"You've been logged out!");
		redirect(base_url(),'refresh'); 	
	 }
	 
	 function requiredmessage() {
	 	$data['title'] = "Require Login";
		$data['main'] = 'site/requiredlogin';
		$this->load->vars($data);
		$this->load->view('site/template','refresh');  
	 }
	 
	 function restriction() {
	 	$data['title'] = "Restricted Access";
		$data['main'] = 'site/restriction';
		$this->load->vars($data);
		$this->load->view('site/template','refresh');  
	 }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */